package interfaces.superWidgets;

import java.util.ArrayList;
import java.util.Collection;

import interfaces.GUISource;

import org.fenggui.ScrollBar;
import org.fenggui.ScrollContainer;
import org.fenggui.StandardWidget;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;

public class HorizontalScrollContent<T extends StandardWidget> extends StaticContent {
		
	protected InterfaceButton leftButton, rightButton;
	protected ScrollContainer scrollContainer;
	protected StaticContent content;
	protected Collection<T> elements;
	protected int elementWidth, elementHeight;
	protected float scrollPercentage;
	
	public HorizontalScrollContent(int width, int height, int x, int y) {
		this(width, height, x, y, height);
	}
	
	public HorizontalScrollContent(int width, int height, int x, int y, int elementWidth) {
		super(width, height, x, y);
		this.elementWidth = elementWidth;
		
		elementHeight = height;
		elements = new ArrayList<T>();
		
		initScrollButtons();
		initContent();
		
		layout();
	}
	
	public void setElementWidth(int elementWidth) {
		this.elementWidth = elementWidth;
	}
	
	protected void initScrollButtons() {
		ImageFont buttonFont = GUISource.middleFont;
		int buttonWidth = getWidth() / 30;
		int buttonHeight = getHeight();
		
		leftButton = new InterfaceButton("<", buttonFont);
		leftButton.setSize(buttonWidth, buttonHeight);
		leftButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				scroll(-scrollPercentage);
			}
		});
		addWidget(leftButton);
		
		rightButton = new InterfaceButton(">", buttonFont);
		rightButton.setSize(buttonWidth, buttonHeight);
		rightButton.setX(getWidth() - buttonWidth);
		rightButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				scroll(scrollPercentage);
			}
		});
		addWidget(rightButton);
	}
	
	private void scroll(float percentage) {
		scrollContainer.scrollHorizontal(percentage);
	}
	
	public void changeButtonWidth(int buttonWidth) {
		leftButton.setWidth(buttonWidth);
		rightButton.setWidth(buttonWidth);
		initContent();
	}
	
	private void initContent() {
		int x = leftButton.getWidth();
		int width = getWidth() - leftButton.getWidth() - rightButton.getWidth();
		
		scrollContainer = new ScrollContainer();
		scrollContainer.setX(x);
		scrollContainer.setMinSize(width, height);
		scrollContainer.setSizeToMinSize();
		GUISource.setTheme(scrollContainer);
		
		setupScrollBar(scrollContainer.getHorizontalScrollBar());
		setupScrollBar(scrollContainer.getVerticalScrollBar());
		
		addWidget(scrollContainer);
		
		layout();
		
		content = new StaticContent(elementWidth, elementHeight, 0, 0);
		content.setMinSize(content.getSize());
		scrollContainer.setInnerWidget(content);
	}
	
	private void setupScrollBar(ScrollBar bar) {
		GUISource.setTheme(bar);
		bar.setMinSize(0, 0);
		bar.setSizeToMinSize();
		bar.setVisible(false);
		bar.setEnabled(false);
	}
	
	public void addElement(T element) {
		element.setSize(elementWidth, elementHeight);
		element.setX(elements.size() * elementWidth);
		elements.add(element);
		content.addWidget(element);
		
		int newContentSize = elements.size() * elementWidth;
		if(content.getWidth() < newContentSize) {
			content.setMinSize(newContentSize, height);
			content.setSizeToMinSize();
		}
		
		scrollPercentage = elementWidth / (float)content.getWidth();
		
		layout();
	}
	
	public Collection<T> getElements() { return elements; }
	
	public int getElementWidth() { return elementWidth; }
	
	public int getViewWidth() { return getWidth() - leftButton.getWidth() - rightButton.getWidth(); }
}